package com.eedi.framework.notify.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.notify.controller.sys.vo.SysNoticeSaveReq;
import com.eedi.framework.notify.controller.sys.vo.SysNoticeUpReq;
import com.eedi.framework.notify.service.SysNoticeService;
import com.eedi.framework.notify.controller.sys.vo.SysNoticeResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.notify.controller.sys.vo.SysNoticePageReq;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 通知公告")
@RestController
@RequestMapping("/notice")
@Validated
public class SysNoticeController {

    @Resource
    private SysNoticeService sysNoticeService;

//    @Resource
//    private WebSocketSenderApi webSocketSenderApi;

    @PostMapping("/create")
    @Operation(summary = "创建通知公告")
    @PreAuthorize("@sys.hasPermission('sys:notice:create')")
    public CommonResult<String> createNotice(@Valid @RequestBody SysNoticeSaveReq createReqVO) {
        String noticeId = sysNoticeService.createNotice(createReqVO);
        return success(noticeId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改通知公告")
    @PreAuthorize("@sys.hasPermission('sys:notice:update')")
    public CommonResult<Boolean> updateNotice(@Valid @RequestBody SysNoticeUpReq updateReqVO) {
        sysNoticeService.updateNotice(updateReqVO);
        return success(true);
    }

    @PutMapping("/enable")
    @Operation(summary = "开启")
    @PreAuthorize("@sys.hasPermission('sys:notice:enable')")
    public CommonResult<Boolean> enable(@RequestParam("sysNoticeId")String sysNoticeId) {
        sysNoticeService.enable(sysNoticeId);
        return success(true);
    }

    @PutMapping("/disable")
    @Operation(summary = "关闭")
    @PreAuthorize("@sys.hasPermission('sys:notice:disable')")
    public CommonResult<Boolean> disable(@RequestParam("sysNoticeId")String sysNoticeId) {
        sysNoticeService.disable(sysNoticeId);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通知公告")
    @Parameter(name = "sysNoticeId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:notice:delete')")
    public CommonResult<Boolean> deleteNotice(@RequestParam("sysNoticeId") String sysNoticeId) {
        sysNoticeService.deleteNotice(sysNoticeId);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获取通知公告列表")
    @PreAuthorize("@sys.hasPermission('sys:notice:query')")
    public CommonResult<PageResult<SysNoticeResp>> getNoticePage(@Validated SysNoticePageReq pageReqVO) {
        return success( sysNoticeService.getNoticePage(pageReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得通知公告")
    @Parameter(name = "sysNoticeId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@sys.hasPermission('sys:notice:query')")
    public CommonResult<SysNoticeResp> getNotice(@RequestParam("sysNoticeId") String sysNoticeId) {
        return success(sysNoticeService.getNotice(sysNoticeId));
    }



//    @PostMapping("/push")
//    @Operation(summary = "推送通知公告", description = "只发送给 websocket 连接在线的用户")
//    @Parameter(name = "sysNoticeId", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('sys:notice:update')")
//    public CommonResult<Boolean> push(@RequestParam("sysNoticeId") String sysNoticeId) {
//        SysNoticeDO notice = sysNoticeService.getNotice(sysNoticeId);
//        Assert.notNull(notice, "公告不能为空");
//        // 通过 websocket 推送给在线的用户
////        webSocketSenderApi.sendObject(UserTypeEnum.MEMBER_ADMIN.getCode(), "notice-push", notice);
//        return success(true);
//    }

}
